{extend name='admin@main'}

{block name="content"}

<form onsubmit="return false;" id="GoodsForm" data-auto="true" method="post" class='layui-form layui-card' autocomplete="off">
    <div class="layui-card-body think-box-shadow padding-left-40">
        <div class="layui-form-item">
            <span class="color-green label-required-prev">碎片名称</span>
            <input name="name" required class="layui-input" placeholder="请输入碎片名称" {if (isset($vo.id)&&in_array($vo.id,[1,2,3,4]))}disabled {/if} value="{$vo.name|default=''}">
        </div>
        <div class="layui-form-item layui-row layui-col-space15 relation_goods">
            <label class="layui-col-xs3 relative">
                <span class="color-green">关联类型</span>
                <select name='type' class='layui-select full-width' lay-filter="type" >
                    {foreach $type as $ty_key=>$ty}
                    {if isset($vo.type) and $vo.type eq $ty_key}
                    <option selected value="{$ty_key}" >{$ty|default=''}</option>
                    {else}
                    <option value="{$ty_key}" >{$ty|default=''}</option>
                    {/if}
                    {/foreach}
                </select>
            </label>
        </div>

        <div class="layui-form-item blockList" id="block_1">
            <span class="color-green label-required-prev">碎片内容</span>
            <input  required class="layui-input blocks block_1"  placeholder="请输入碎片内容" value="{$vo.content|default=''}">
        </div>

        <div class="layui-form-item blockList" id="block_2">
            <span class="color-green label-required-prev">碎片内容</span>
            <textarea  class=" blocks block_2 layui-textarea">{$vo.content|raw|default=''}</textarea>
        </div>

        <div class="layui-form-item blockList" id="block_3">
            <span class="color-green label-required-prev">展示图片</span>
            <div><input id="image" type="hidden" value="{$vo.content|default=''}" class="blocks block_3"></div>
            <script> $('#image').uploadOneImage();</script>

        </div>

        <div class="layui-form-item blockList" id="block_4">
            <span class="color-green label-required-prev">碎片内容</span>
            <textarea  class="layui-textarea blocks block_4">{$vo.content|raw|default=''}</textarea>
        </div>

        <div class="layui-form-item text-center">
            {notempty name='vo.id'}<input type="hidden" name="id" value="{$vo.id}">{/notempty}
            <!--<button class="layui-btn layui-btn-danger" onclick="hsitoryBack()" type="button">取消编辑</button>-->
            <button class="layui-btn" type="submit">保存碎片</button>
        </div>

    </div>
</form>
{/block}

{block name='script'}
<script>

    $(document).ready(function(){
        $(".blockList").hide();  //初始化所有内容隐藏
        var ty_key="{$vo.type|default=0}"
        if(ty_key!=0){                 //初始化内容
            $("#block_"+$("[name=type]").val()).show();
            $(".block_"+$("[name=type]").val()).attr('name','content');
            // $('#image').uploadOneImage();
        }

        window.form.render();
        layui.use(['jquery','table','form'], function(){
            var table = layui.table,
                $ = layui.jquery,
                form = layui.form;

            //保存按钮
            form.on('select(type)', function(data){
                var val=data.value;
                $(".blockList").hide();
                $(".blocks").attr('name','');
                $("#block_"+val).show();
                $(".block_"+val).attr('name','content');
                if(val==3){
                    $('.uploadimage').attr('style','background-image: url(/static/theme/img/image.png)');
                }
                window.form.render();
            });
        });


    });
    require(['ckeditor'], function () {
        window.createEditor('.block_4', {height: 500});
    })
</script>
{/block}
